package com.example.chat.api;

import com.example.chat.model.Friend;
import com.example.chat.model.FriendMapper;
import com.example.chat.model.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
public class FriendAPI {

    @Resource
    private FriendMapper friendMapper;

    @GetMapping("/friendList")
    @ResponseBody
    public Object getFriendList(HttpServletRequest req) {

        HttpSession session = req.getSession(false);
        if (session == null) {
            System.out.println("[getFriendList]: 当前请求中，session不存在");
            return new ArrayList<Friend>();
        }

        User user = (User)session.getAttribute("user");
        if (user == null) {
            System.out.println("[getFriendList]: 当前会话中，user不存在");
            return new ArrayList<Friend>();
        }

        List<Friend> friendList = friendMapper.selectFriendList(user.getUserId());

        return friendList;
    }
}
